<?php
class CouponAction extends CommonAction{
	public function index(){
		$member_id = $this->memberInfo['id'];
		$this->assign("member_id",$member_id);
		
		
		$this -> is_subscribe = M('Member') -> where(array('id' => $member_id)) -> find();
		if($this -> is_subscribe['is_subscribe'] == 0){
			if(C('URL_MODEL') == 2){
				 $jumpUrl = str_replace('index.php/','',__SELF__);
			}else{
				 $jumpUrl = __SELF__;
			}
			F('subscribe_returnUrl'.$this -> is_subscribe['openid'], '<a href="http://' . $_SERVER['HTTP_HOST'] .$jumpUrl.'">点击这里领取优惠券</a>');
			
		}
		
		
		$this->assign("site_id",__SITEID__);

		$coupon_id = intval($_GET['id']);
		if($coupon_id){
			$this->assign("coupon_id",$coupon_id);
 
			import("@.ORG.Coupon.Coupon");
			$Coupon = new Coupon($coupon_id,"0");
			$this->assign("coupon",$Coupon->coupon);
			if(__SITEID__ == $Coupon->site_id){
				// 查看该用户是否拥有优惠券
				$myCoupons = $Coupon->myCoupons($member_id);
				$this->assign('myCoupons',$myCoupons);
				// exit(dump($myCoupons));
				// 获得存票数量
				$exist = $Coupon->exist();
	
				$this->assign("exist",$exist);
				$this->assign("canUse",$Coupon->canUse());
				// 展示领取页面					
				$this->display("index1");
			}else{
				$this->error("不能领取其他网站的优惠券");
			}
		}else{
			import("@.ORG.Coupon.Coupon");
			$Coupon = new Coupon();
			$Coupon->site_id = __SITEID__;

			$myCoupons = $Coupon->myCoupons($member_id);
		   	$this->assign('myCoupons',$myCoupons);

			$this->display("index2");
		}
		
	}
	public function receive(){
		$member_id = $this->memberInfo['id'];
		$coupon_id = $_POST['coupon_id'];
		
		$this -> is_subscribe = M('Member') -> where(array('id' => $member_id)) -> find();
		if($this -> is_subscribe['is_subscribe'] == 0){
			$this->error('请先关注公众号');exit; 
			
		} 
		
		import("@.ORG.Coupon.Coupon");
		$Coupon = new Coupon($coupon_id,"0");

		if(__SITEID__ == $Coupon->site_id){
			if($Coupon->canTake($member_id)){
				$return = $Coupon->getCoupon($member_id);
				if($return){
					$this->success('领取成功',U('index',array('id'=>$coupon_id)));
				}else{
					$this->error("领取失败",U('index',array('id'=>$coupon_id)));
				}
			}else{
				$this->error("领取失败",U('index',array('id'=>$coupon_id)));
			}
		}else{
			$this->error("不能领取其他网站的优惠券",U('index',array('id'=>$coupon_id)));
		}
	}

	public function receive_sn(){
		$member_id = $_POST['member_id'];

		$sn_sn = trim($_POST['sn']);
		$count_map['site_id'] = $map['site_id'] = $_POST['site_id'];
		$map['sn'] = $sn_sn;
		$count_map['member_id'] = $member_id;

		// $sn = M('coupon_sn')->where($map)->find();
		import("@.ORG.Coupon.Coupon");
		$Coupon = new Coupon($sn_sn,"2");
		$sn = $Coupon->coupon_sn;
		$coupon = $Coupon->coupon;
		$count_map['coupon_id'] = $coupon['id'];
		// coupon可用，coupon剩余，类型为商家线下发放类型，不超过最大领取数
		if($sn['member_id']){
			$this->error("该优惠券已被领取",U('index'));
		}elseif($Coupon->canUse()
		 && ($Coupon->exist() > 0)
		  && $coupon['type'] == "2"
		   && $coupon['max_num'] > M('coupon_sn')->where($count_map)->count()){

			$data['member_id'] = $member_id;
			$data['ctime'] = time();
			$result = M('coupon_sn')->where($map)->setField($data);
			if($result){
				$this->success("领取成功",U('index'));
			}else{
				$this->error("领取失败",U('index'));
				// $this->error(M('coupon_sn')->getLastSql(),U('index'));
			}
		}else{
			// dump(M('coupon_sn')->getLastSql());
			$this->error("超额领取或优惠券失效",U('index'));
		}
	}

}